<html>
<head>
    <meta http-equiv="Pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-cache">
    <meta http-equiv="Content-Language" content="zh-cn">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>题目同步</title>
</head>
<body leftmargin="30">
<center>
    <?php
    require_once("../config/path_config.php");
    require_once("../include/db_info.inc.php");
    require_once("admin-header.php");
    $allowAccess = isset($_SESSION['administrator']);
    $allowAccess |= isset($_SESSION['problem_editor']);
    if ( ! $allowAccess ){
        echo "<a href='../loginpage.php'>Please Login First!</a>";
        exit(1);
    }
    include_once("kindeditor.php") ;
    ?>
    <?php
    require_once("../include/simple_html_dom.php");
    $url=$_POST ['url'];
    if ( !$url ) {
        $url=$_GET['url'];
    }
    if (strpos($url, "http") === false){
        echo "Please Input like http://acm.hdu.edu.cn/showproblem.php?pid=1994";
        exit(1);
    }

    $str = file_get_contents($url);
    // 避免中文乱码的问题
    $html = iconv("gb2312", "utf-8//IGNORE", $str);

    //获取标题
    preg_match("/<td align=center>[\s\S]+style[\s\S]+>([\S\s]+)<\/h1>[\s\S]+Time Limit/i", $html, $match);
    $title = $match[1];

    // 获取题目描述
    preg_match("/Problem\s*Description[\s\S]+panel_content>([\s\S]+?)<br>\s*<\/div>[\s\S]+left>Input/i", $html, $match);
    $desc = $match[1];

    // 获取输入格式
    preg_match("/left>Input[\s\S]+panel_content>([\s\S]+?)<br>(\s*<\/div>\s*<div class=panel_bottom>?)[\s\S]+left>Output/i", $html, $match);
    $input = $match[1];

    // 获取输出格式
    preg_match("/left>Output[\s\S]+panel_content>([\s\S]+)<br>\s*<\/div>[\s\S]+left>Sample\sInput/i", $html, $match);
    $output = $match[1];

    // 获取输入样例
    preg_match("/Sample\s*Input[\s\S]+<pre>[\s\S]+>([\s\S]+)<\/div><\/pre>[\s\S]+Sample\sOutput/i", $html, $match);
    $sampleInput = $match[1];

    // 获取输出样例
    preg_match("/Sample\s*Output[\s\S]+<pre>[\s\S]+>([ \s \S]+)<\/div>\s*<\/pre>/i", $html, $match);
    $sampleOnput = $match[1];

    // 获取说明
    //preg_match("/<h2>说明<\/h2>\s*<div>\s*([\s\S]+?)<\/div>/i", $html, $match);
    $hint = "";
    /*
    if (isset($match[1]) ) {
        $hint = $match[1];
    }
    $hint= str_replace("<p>","", $hint);
    $hint = str_replace("</p>","<br/>", $hint);
    $hint = str_replace("<br/><br/>","<br/>", $hint);
    */
    ?>
    <form method=POST action=problem_add.php>
        <p align=center><font size=4 color=#333399>Add a Problem</font></p>
        <input type=hidden name=problem_id value=New Problem>
        <p align=left>题目ID:&nbsp;&nbsp;New Problem</p>
        <p align=left>标题:<input type=text name=title size=71 value="<?php echo $title?>"></p>
        <p align=left>时间限制:<input type=text name=time_limit size=20 value="1">S</p>
        <p align=left>内存限制:<input type=text name=memory_limit size=20 value="128">MByte</p>
        <p align=left>题目描述:<br>
            <textarea class="kindeditor" rows=13 name=description cols=80>
                <?php echo $desc;?>
            </textarea>
        </p>
        <p align=left>输入:<br>
            <textarea class="kindeditor" rows=13 name=input cols=80>
                <?php echo $input;?>
            </textarea>
        </p>
        </p>
        <p align=left>输出:<br>
            <textarea class="kindeditor" rows=13 name=output cols=80>
                <?php echo $output;?>
            </textarea>
        </p>
        <p align=left>样例输入:<br>
            <textarea rows=13 name=sample_input cols=80><?php echo $sampleInput?></textarea>
        </p>
        <p align=left>样例输出:<br>
            <textarea rows=13 name=sample_output cols=80><?php echo $sampleOnput?></textarea>
        </p>
        <p align=left>提示:<br>
            <textarea class="kindeditor" rows=13 name=hint cols=80>
                <?php echo $hint;?>
            </textarea>
        </p>
        <p align=left>SpecialJudge:
            N<input type=radio name=spj value='0' checked>
            Y<input type=radio name=spj value='1'>
        </p>
        <p align=left>题目类别:<br>
            <textarea name=source rows=1 cols=70>未知</textarea>
        </p>
        <p align=left>
            <font color="blue">分为：程序设计基础、数据结构、基础算法3个类别9个等级。如：程序设计基础（一维数组、冒泡排序）</font>
        </p>
        <p align=left>
            题目来源:<br>
            <textarea name=fromoj rows=1 cols=70>HDU<?php echo substr($url, -4); ?></textarea>
        </p>
        <p align=left>所属比赛:
            <select  name=contest_id>
                <option value=''>none</option>
            </select>
        </p>
        <div align=center>
            <?php require_once("../include/set_post_key.php");?>
            <input type=submit value=提交 name=submit>
        </div></form>
    <p>

</body></html>
